package view;

import Dao.OriginalDataDao;
import Model.originalData;
import Utils.DBUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

public class originalData_ReviseFrm extends JFrame{
    private JPanel panel1;
    private JTextField textField_ENTitle;
    private JTextField textField_CNTitle;
    private JTextField textField_UID;
    private JTextField textField_Version;
    private JTextField textField_Keywords;
    private JTextField textField_Abstract;
    private JTextField textField_Content;
    private JTextField textField_Sort;
    private JTextField textField_Author;
    private JTextField textField_Affiliation;
    private JTextField textField_Tag;
    private JTextField textField_Location;
    private JTextField textField_Memo;
    private JTextField textField_Catalog;
    private JButton OkButton;
    private JButton ResetButton;
    private JButton CancelButton;
    private originalData originalData;
    private originalDataFrm originalDataFrm;

    private DBUtils dbUtils = new DBUtils();
    private Dao.OriginalDataDao originalDataDao = new OriginalDataDao();
    public Connection conn;

    originalData_ReviseFrm(Connection connection,originalData originalData,originalDataFrm originalDataFrm) {
        setContentPane(panel1);
        setVisible(true);
        setBounds(50, 20, 650, 500);
        //建立连接
        try {
            //用于建立对于连接
            this.conn = connection;
            //用于将上个窗口选中的记录信息传入本窗口使用
            this.originalData=originalData;
            //用于添加完调取原始数据窗口的填充表格函数
            this.originalDataFrm=originalDataFrm;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //填充文本框
        fillTextField();
        //创建监听
        addListen();
    }

    //将选中记录的信息填充到本窗口的文本框中
    private void fillTextField()
    {
        textField_ENTitle.setText(originalData.getEgTitle());
        textField_CNTitle.setText(originalData.getCnTitle());
        textField_UID.setText(originalData.getUID());
        textField_Version.setText(originalData.getVersion());
        textField_Keywords.setText(originalData.getKeyWord());
        textField_Abstract.setText(originalData.getAbstract());
        textField_Content.setText(originalData.getContent());
        textField_Sort.setText(originalData.getSort());
        textField_Author.setText(originalData.getAuthor());
        textField_Affiliation.setText(originalData.getCompany());
        textField_Tag.setText(originalData.getLabel());
        textField_Location.setText(originalData.getLocation());
        textField_Memo.setText(originalData.getMemo());
        String c=Integer.toString(originalData.getCatalog());
        textField_Catalog.setText(c);
    }

    //创建监听
    private void addListen()
    {
        //修改
        OkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ReviseActionPerformed(e);
            }
        });

        //重置
        ResetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resetActionPerformed(e);
            }
        });

        //关闭窗口
        CancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    //更新修改
    private void ReviseActionPerformed(ActionEvent e)
    {
        //将当前文本框的信息获取存入
        String EgName=textField_ENTitle.getText();
        String CnTitle=textField_CNTitle.getText();
        String  UID=textField_UID.getText();
        String Version=textField_Version.getText();
        String KeyWord=textField_Keywords.getText();
        String Abstract=textField_Abstract.getText();
        String Content=textField_Content.getText();
        String Sort=textField_Sort.getText();
        String Author=textField_Author.getText();
        String Company=textField_Affiliation.getText();
        String Label=textField_Tag.getText();
        String Location=textField_Location.getText();
        String Memo=textField_Memo.getText();
        String Catalog=textField_Catalog.getText();
        int c=Integer.parseInt(Catalog);

        //将相应值赋予对象
        originalData.setEgTitle(EgName);
        originalData.setCnTitle(CnTitle);
        originalData.setUID(UID);
        originalData.setVersion(Version);
        originalData.setKeyWord(KeyWord);
        originalData.setAbstract(Abstract);
        originalData.setContent(Content);
        originalData.setSort(Sort);
        originalData.setAuthor(Author);
        originalData.setCompany(Company);
        originalData.setLabel(Label);
        originalData.setLocation(Location);
        originalData.setMemo(Memo);
        originalData.setCatalog(c);

        try {
            //修改
            int num= originalDataDao.update(conn,originalData);
            //对应系统提示框弹出
            if(num>0){
                JOptionPane.showMessageDialog(null,"修改成功");
            }
            else {
                JOptionPane.showMessageDialog(null,"修改失败");
            }
            //调取原始数据窗口函数——更新当前表格记录
            originalDataFrm.fillTable(new originalData());
            dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(ex);
        }
    }

    //重置文本框函数
    private void resetActionPerformed(ActionEvent e)
    {
        textField_ENTitle.setText("");
        textField_CNTitle.setText("");
        textField_UID.setText("");
        textField_Version.setText("");
        textField_Keywords.setText("");
        textField_Abstract.setText("");
        textField_Content.setText("");
        textField_Sort.setText("");
        textField_Author.setText("");
        textField_Affiliation.setText("");
        textField_Tag.setText("");
        textField_Location.setText("");
        textField_Memo.setText("");
        textField_Catalog.setText("");
    }
}
